package com.hunmengwl.oauth.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

/**
 * @author hunmeng
 * @create 2019-12-30 16:22
 */
@Controller
public class ToHtmlController {


    @GetMapping("/favicon.ico")
    public String favicon() {
        return "redirect:static/images/favicon.ico";
    }

    @GetMapping("/{url}/{page}")
    public ModelAndView html(@PathVariable String url, @PathVariable String page, HttpServletRequest request) {

        if(StringUtils.isNoneBlank(page)){
            url = url + "/" + page;
        }
        return SetParameters(url, request);
    }

    @GetMapping("/{url}")
    public ModelAndView html(@PathVariable String url, HttpServletRequest request) {
        return SetParameters(url, request);
    }

    private ModelAndView SetParameters(String url, HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView(url);
        //传递值
        Enumeration<String> parameterNames = request.getParameterNames();
        while (parameterNames.hasMoreElements()){
            String key = parameterNames.nextElement();
            String parameter = request.getParameter(key);
            modelAndView.addObject(key, parameter);
        }
        return modelAndView;
    }

    @GetMapping("/")
    public String index() {
        return "index";
    }

    @GetMapping("/error/{html}")
    public String index(@PathVariable("html") String html) {
        return "error/" + html;
    }

}
